package com.example.controller;

import Video.Dto.VideoType;
import Video.Dto.Video;
import com.example.mapper.FileClient;
import com.example.mapper.VideoMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pojo.Result;

import java.util.List;

@RestController
@RequestMapping("/video")
@RequiredArgsConstructor
public class VideoController {
    final VideoMapper videoMapper;
    final FileClient fileClient;

    @GetMapping("/type/all")
    public List<String> getAllTypesjol(){
        return videoMapper.getTypesjol();
    }
    //按id查询视频
    @GetMapping("/get-video/{id}")
    public Result<Video> searchVideoById(@PathVariable int id){
        try {
            Video videoById = videoMapper.getVideoById(id);
            if(videoById!=null)
                return Result.success(videoById);
            else {
                return Result.fail("未找到相应视频");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //查询所有视频
    @GetMapping("/all-videos")
    public Result<List<Video>> searchAllVideos() {
        try {
            List<Video> videos = videoMapper.getVideos();
            return Result.success(videos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //根据type_id进行查询分类的视频
    @GetMapping("/get-type/{type_id}")
    public Result<List<Video>> getVideosBySortId(@PathVariable int type_id) {
        try {
            List<Video> videos = videoMapper.getVideosByType(type_id);
            return Result.success(videos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //查询所有分类信息
    @GetMapping("/all-types")
    public Result<List<String>> getAllTypes() {
        try {
            List<String> types = videoMapper.getTypes();
            return Result.success(types);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //根据type查所有包含type字段
    @GetMapping("/types-all")
    public Result<List<VideoType>> getTypesAll() {
        try {
            List<VideoType> types = videoMapper.getTypesAll();
            return Result.success(types);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @GetMapping("/type/{type}")
    public Result<String> getSortByTypeId(@PathVariable int type) {
        try {
            String typeName = videoMapper.getTypeName(type);
            return Result.success(typeName);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //添加视频
    @PostMapping("/add-video")
    public Result<Integer> addVideo(@RequestBody Video video) {
        try {
            videoMapper.insertVideo(video);
            return Result.success(video.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //删除视频
    @PostMapping("/delete-video/{video_id}")
    public Result<?> addVideo(@PathVariable int video_id) {
        try {
            videoMapper.deleteVideos(video_id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

//    @PostMapping("/update")
//    public Result<Integer> updateVideo(@RequestBody Video video) {
//        try {
//            int rowsAffected = videoMapper.updateVideo(video);
//            if (rowsAffected > 0) {
//                return Result.success(rowsAffected);
//            } else {
//                return Result.fail("更新失败，未找到对应简历");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.fail(500, "服务器内部错误");
//        }
//    }


    //添加视频分类
    @PostMapping("/add-type")
    public Result<?> addVideoSort(String sort) {
        try {
            videoMapper.addTypes(sort);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @PostMapping("/delete-type/{type_id}")
    public Result<?> deleteVideoSort(@PathVariable int type_id) {
        try {
            videoMapper.deleteTypes(type_id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

//    @PostMapping("/update-type")
//    public Result<?> updateVideoSort(@RequestBody VideoType videoType) {
//        try {
//            videoMapper.updateTypes(videoType);
//            return Result.success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.fail(500, "服务器内部错误");
//        }
//    }

    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestPart("file") MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            return Result.fail("请选择要上传的文件");
        }
        try {
            String file_url = fileClient.uploadVideos(multipartFile);
            return Result.success(file_url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("文件上传失败");
        }
    }
}
